home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / mdisphist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-20  |  5.3 KB  |  218 lines

  1. /*
  2.     MDISPHIST - display histgram files as a bar graph in multiple ways.
  3. %
  4. % usage:    mdisphist [-m maxcnt] [-F] [-M] [-R] [-g [#]] [-n] [-r] [-w #]
  5. %            [<] mhistogram [> [-o] graphimage]
  6. %
  7. %    Copyright (c)    1991    Jin, Guojun
  8. */
  9. char    usage[]="options    \n\
  10. -m #    specifies an initial maximum bincount for use in scaling the    \n\
  11.     displays.  Otherwise, the maximum in the first histogram is used,\n\
  12.     and changed when selfs is set by -R (Retrieve maxcnt).        \n\
  13. -F [#]    add frame around each histogram. It is good for seeing the histogram,\n\
  14.     but need more calculation for powertool to spread multiple frames.\n\
  15. -M    display more message.    \n\
  16. -R    flag is servered for mhisto using -r option.\n\
  17. -g [#]    grids for both row and column.    \n\
  18. -n    output negative histo-graph.    \n\
  19. -r    will recalculate the maximum count for each frame. If mhisto has\n\
  20.     already used -r option, use -R option in mdisphist is good for saving\n\
  21.     time.    \n\
  22. -w #    specify the output window size. Default is 256 x 256.    \n\
  23.     [<] mhistogram [> graphimage]\n";
  24. /*
  25. @ compile:    cc -O -o mdisphist mdisphst.c -lccs -lhipsh -lhips -lm
  26. @
  27. @ AUTHOR:    Guojun Jin - Lawrence Berkeley Laboratory    1/20/91
  28. @ changes:
  29. @    2/7/91    hist buffer <= MType
  30. @    6/17/91    add thicker frame
  31. */
  32.  
  33. #include "header.def"
  34. #include "imagedef.h"
  35. #include <math.h>
  36.  
  37. U_IMAGE    uimg;
  38. #define    buf    uimg.src
  39.  
  40. #define    border    127
  41. #define    bground    0
  42. #define    bar    255
  43.  
  44. #ifndef    Visual
  45. #define    Visual    48
  46. #endif
  47.  
  48. #define    grid    border + Visual
  49. #define    GValue()    arget(argc, argv, &r, &c)
  50.  
  51. #ifdef    _DEBUG_
  52. extern    int    debug;
  53. #endif
  54.  
  55. bool    grids, frac_grid, neg,
  56.     selfs,
  57.     ofsz,
  58.     recalc,
  59.     BFRAME,
  60.     Msg;
  61.  
  62.  
  63. main(argc, argv)
  64. int    argc;
  65. char**    argv;
  66. {
  67. MType    k, r, c, f, fr,
  68.     numbin, binwidth,
  69.     maxcnt=0, goff=0,    /* grid offset */
  70.     *hist, image_size=256, image_frame;
  71. byte    Bar;
  72. float    ratio,    /*     window compress ratio    */
  73.     factor;    /*    window enlarge factor    */
  74.  
  75. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "S16-1");
  76.  
  77. for (r=1; r<argc; r++)
  78.     if (*argv[r] == '-')    {
  79.     c = 1;
  80.     switch(argv[r][c++]) {
  81. #ifdef    _DEBUG_
  82.     case 'D':debug++;    break;
  83. #endif
  84.     case 'F':BFRAME = GValue();
  85.         if (BFRAME<1 || BFRAME>8)    BFRAME=1;
  86.         break;
  87.     case 'M':    Msg++;    break;
  88.     case 'R':    selfs++;    break;
  89.     case 'm':
  90.         maxcnt = GValue();    break;
  91.     case 'g':
  92.         grids = GValue();
  93.         if (!grids)    grids = 8;
  94.         break;
  95.     case 'n':
  96.         neg++;    break;
  97.     case 'r':
  98.         recalc++;    break;
  99.     case 'w':
  100.         ofsz = GValue();
  101.         if (!ofsz)    ofsz--;
  102.         break;
  103.     default:
  104. errout:        usage_n_options(usage, r, argv[r]);
  105.     }
  106.     }
  107.     else if (!freopen(argv[r], "rb", stdin))
  108.         syserr("%s -- not found", argv[r]);
  109.  
  110. io_test(stdin_fd, goto    errout);
  111.  
  112. (*uimg.header_handle)(HEADER_READ, &uimg, 0, 0);
  113. r = uimg.height;
  114. c = uimg.width;
  115. f = uimg.frames;
  116.  
  117. if (uimg.in_form != IFMT_HIST)
  118.     syserr("image must be in histogram format");
  119. uimg.o_form = IFMT_BYTE;
  120. uimg.pxl_out = 1;
  121.  
  122. if (ofsz)
  123.    if (ofsz<0)
  124.     image_size <<= 1;
  125.    else    image_size = ofsz;
  126. image_frame = image_size + (BFRAME<<1);
  127. uimg.width = uimg.height = image_frame;
  128. if (grids){
  129.     grids = image_size / grids;
  130.     frac_grid = image_size % grids;
  131.     goff = BFRAME;
  132. }
  133. else    grids = frac_grid = image_frame;
  134.  
  135. if (upread(&binwidth, 1, sizeof(binwidth), stdin) != sizeof(binwidth))
  136.     syserr("error during read bin width");
  137. if (upread(&numbin, 1, sizeof(numbin), stdin) != sizeof(numbin))
  138.     syserr("error during read bin number");
  139.  
  140. (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, True);
  141.  
  142. hist = zalloc(numbin, sizeof(*hist), "hist");
  143. buf = nzalloc(image_frame, image_frame, "buf");
  144.  
  145. if (Msg){
  146.     message("%s: image size was %d x %d\n", *argv, r, c);
  147.     message("%s: there are %d (width=%d)bins ## grid : size=%d, frac=%d\n",
  148.         *argv, numbin, binwidth, grids, frac_grid);
  149. }
  150.  
  151. for(fr=0; fr<f; fr++) {
  152. register int    i, j;
  153. register byte*    bp = buf;
  154.     for (i=0; i<image_frame; i++)
  155.         for (j=0; j<image_frame; j++)
  156.         if (!j || j==image_frame-1 || !i || i==image_frame-1)
  157.             *bp++ = border;
  158.         else if (((i-frac_grid-goff) % grids) && ((j-goff) % grids))
  159.             *bp++ = bground;
  160.         else
  161.             *bp++ = grid;
  162.  
  163.     if (upread(&k, 1, sizeof(k), stdin) != sizeof(k))
  164.         syserr("can not read max count");
  165.     if (selfs || !maxcnt)    maxcnt = k;
  166.     if (upread(hist, sizeof(*hist), numbin, stdin) != numbin)
  167.         syserr("error during read histgram");
  168.     if (recalc || !maxcnt){
  169.         register MType    max=0, *hp=hist;
  170.         for (i=0; i<numbin; i++, hp++)
  171.             if (*hp > max)    max = *hp;
  172.         maxcnt = max;
  173.         if (Msg)
  174.             message("maximum count in this frame is %d\n", max);
  175.     }
  176.     if (numbin < image_size){
  177.         ratio = 1;
  178.         factor = (float)image_size/numbin;    /* need repeating */
  179.     }
  180.     else{    ratio = (float)numbin / image_size;
  181.         factor = 1;    /* need compress    */
  182.     }
  183.     Bar = bar / ratio;
  184.     if (Bar < Visual)
  185.         Bar = Visual;
  186.     if (Msg)
  187.     message("%s: frame %d maxcnt increased to %d :: diagram compress %.2f\n",
  188.         *argv, fr, maxcnt, ratio);
  189.  
  190.     for (i=0; i<image_size; i++)    /* horizontal process */
  191.         for (r=0; r<ratio; r++){    /* Accumulate bar for compress window */
  192.         bp = buf;
  193. #ifdef ULORIG
  194.         bp += i + (image_frame-1)*BFRAME;
  195. #else
  196.         bp += i + (image_frame-1)*(image_size+BFRAME);
  197. #endif
  198.         j = image_size * hist[(int)(i*ratio/factor + r)] / maxcnt;
  199.         if (j > image_size)    j = image_size;    /* bra height    */
  200.  
  201.         for (k=0; k<j; k++) {    /* vertical process    */
  202.             if (Bar + *bp > 255)    *bp = 255;
  203.             else    *bp += Bar;
  204. #ifdef ULORIG
  205.             bp += image_frame;
  206. #else
  207.             bp -= image_frame;
  208. #endif
  209.         }
  210.         }
  211.     j = image_frame * image_frame;
  212.     if (neg) for (i=j, bp=buf; i--; bp++)    *bp = -1 - *bp;
  213.     i = fwrite(buf, 1, j, stdout);
  214.     if (i != j)    syserr("error during write [%d] %d", j, i);
  215. }
  216. exit(0);
  217. }
  218.